package com.ruoyi.leavetime.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.employeeInfomation.domain.SalaryEmployee;
import com.ruoyi.employeeInfomation.mapper.SalaryEmployeeMapper;
import com.ruoyi.leavetime.vo.LeaveTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.leavetime.mapper.SalaryLeaveTimeMapper;
import com.ruoyi.leavetime.domain.SalaryLeaveTime;
import com.ruoyi.leavetime.service.ISalaryLeaveTimeService;

/**
 * 请假时间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-12
 */
@Service
public class SalaryLeaveTimeServiceImpl extends ServiceImpl<SalaryLeaveTimeMapper, SalaryLeaveTime> implements ISalaryLeaveTimeService {
    @Autowired
    private SalaryLeaveTimeMapper salaryLeaveTimeMapper;
    @Autowired
    private SalaryEmployeeMapper salaryEmployeeMapper;
    /**
     * 查询请假时间
     *
     * @param id 请假时间主键
     * @return 请假时间
     */
    @Override
    public SalaryLeaveTime selectSalaryLeaveTimeById(Long id) {
        return salaryLeaveTimeMapper.selectSalaryLeaveTimeById(id);
    }

    /**
     * 查询请假时间列表
     *
     * @param salaryLeaveTime 请假时间
     * @return 请假时间
     */
    @Override
    public List<SalaryLeaveTime> selectSalaryLeaveTimeList(SalaryLeaveTime salaryLeaveTime) {
        return salaryLeaveTimeMapper.selectSalaryLeaveTimeList(salaryLeaveTime);
    }

    /**
     * 新增请假时间
     *
     * @param salaryLeaveTime 请假时间
     * @return 结果
     */
    @Override
    public int insertSalaryLeaveTime(SalaryLeaveTime salaryLeaveTime) {
                salaryLeaveTime.setCreateTime(DateUtils.getNowDate());
            return salaryLeaveTimeMapper.insertSalaryLeaveTime(salaryLeaveTime);
    }

    /**
     * 修改请假时间
     *
     * @param salaryLeaveTime 请假时间
     * @return 结果
     */
    @Override
    public int updateSalaryLeaveTime(SalaryLeaveTime salaryLeaveTime) {
                salaryLeaveTime.setUpdateTime(DateUtils.getNowDate());
        return salaryLeaveTimeMapper.updateSalaryLeaveTime(salaryLeaveTime);
    }

    /**
     * 批量删除请假时间
     *
     * @param ids 需要删除的请假时间主键
     * @return 结果
     */
    @Override
    public int deleteSalaryLeaveTimeByIds(Long[] ids) {
        return salaryLeaveTimeMapper.deleteSalaryLeaveTimeByIds(ids);
    }

    /**
     * 删除请假时间信息
     *
     * @param id 请假时间主键
     * @return 结果
     */
    @Override
    public int deleteSalaryLeaveTimeById(Long id) {
        return salaryLeaveTimeMapper.deleteSalaryLeaveTimeById(id);
    }

    @Override
    public void addLeaveTime(LeaveTimeVo leaveTimeVo) throws ParseException {
        SalaryEmployee salaryEmployee = new SalaryEmployee();
        SalaryLeaveTime salaryLeaveTime = new SalaryLeaveTime();

        List<SalaryEmployee> list = salaryEmployeeMapper.selectSalaryEmployeeList(salaryEmployee);
        for (SalaryEmployee emp:list) {
            if(leaveTimeVo.getJobNumber().equals(emp.getJobNumber())){
                salaryLeaveTime.setEmployeeId(emp.getId());
            }
        }
        BeanUtil.copyProperties(leaveTimeVo,salaryLeaveTime);

        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        //时间格式转换
        Date start =  leaveTimeVo.getLeaveStartTime();
         Date end= leaveTimeVo.getLeaveEndTime();
        String starttime = sim.format(start);
        String endtime = sim.format(end);
        int days  = getWorkingDaysBetweenDates(starttime, endtime);
        salaryLeaveTime.setLeaveDays(String.valueOf(days));
        salaryLeaveTimeMapper.insertSalaryLeaveTime(salaryLeaveTime);
    }
    public static int getWorkingDaysBetweenDates(String startDateStr, String endDateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(startDateStr);
        Date endDate = sdf.parse(endDateStr);

        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);

        int workingDays = 0;
        while (startCal.before(endCal) || startCal.equals(endCal)) {
            if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                workingDays++;
            }
            startCal.add(Calendar.DAY_OF_MONTH, 1);
        }

        return workingDays;

    }
}